// import Vue from 'vue';
import Axios from 'axios';
// import wx from 'weixin-js-sdk';
import shareImg from '@/assets/images/shareImg.png';

export const wechatAuth = async (authUrl, device, allowShare) => {
    let shareConfig = {
        title: "防疫要宣传 天域在线选",
        desc: "宣传物资在线,选消毒封装送上门",
        link: allowShare ? authUrl : window.location.origin,
        imgUrl: window.location.origin + shareImg
    };

    // let authRes = await http.get("/pfront/wxauth/jsconfig", {
    //     params: {
    //         url: decodeURIComponent(device == "ios" ? window.entryUrl : authUrl)
    //     }
    // });
    let authRes = await getSignParams(authUrl, device, allowShare);

    if (authRes && authRes.code == 1) {
        wx.config({
            //debug: true,
            appId: authRes.data.appId,
            timestamp: authRes.data.timestamp,
            nonceStr: authRes.data.nonceStr,
            signature: authRes.data.signature,
            jsApiList: ["updateAppMessageShareData", "updateTimelineShareData", "onMenuShareAppMessage", "onMenuShareTimeline"]
        });

        wx.ready(() => {
            wx.updateAppMessageShareData({
                title: shareConfig.title,
                desc: shareConfig.desc,
                link: shareConfig.link,
                imgUrl: shareConfig.imgUrl,
                success: function () {//设置成功
                    //shareSuccessCallback();
                }
            });
            wx.updateTimelineShareData({
                title: shareConfig.title,
                link: shareConfig.link,
                imgUrl: shareConfig.imgUrl,
                success: function () {//设置成功
                }
            });
            wx.onMenuShareTimeline({
                title: shareConfig.title,
                link: shareConfig.link,
                imgUrl: shareConfig.imgUrl,
                success: function () {
                }
            });
            wx.onMenuShareAppMessage({
                title: shareConfig.title,
                desc: shareConfig.desc,
                link: shareConfig.link,
                imgUrl: shareConfig.imgUrl,
                success: function () {
                }
            });
        });
        wx.error(function (res) {
            // console.log('wx.error: ' + res.errMsg);
            let u = navigator.userAgent;
            let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
            if(res.errMsg.indexOf('config:ok') === -1 && !isAndroid) {
                // window.location.href = allowShare ? authUrl : window.location.origin;
                let _url = allowShare ? authUrl : window.location.origin;
                window.location.replace(_url);
            }
        });
    }
};

//异步接口获取签名
function getSignParams(authUrl, device, allowShare) {
  return new Promise((resolve, reject) => {
        let config = {
            headers:{'Content-Type': 'multipart/form-data'}
        };  //添加请求头
        Axios.get(`${window.config['WxSignParams']}`, {
            params: {
                url: decodeURIComponent(device == "ios" ? window.entryUrl : authUrl)
            }
        }, config)
        .then(response => {
            resolve(response.data);
        })
        .catch(e => {
            reject(e);
        });
    })      
}